//
//  HUD.swift
//  aquariumstudio
//
//  Created by zj on 2022/10/13.
//  Copyright © 2022 LXF. All rights reserved.
//

import Foundation
import SVProgressHUD
import FastComponent

private var XX_Ex_Tag_Key: String = "FastComponent.XX_Ex_Tag_Key.hudVersion"
fileprivate extension XXExtension where Base:NSObject{
    
    /// 附属物品  - RETAIN_NONATOMIC
    var hudVersion:Int {
        get {
            if let v = objc_getAssociatedObject(self.base, &XX_Ex_Tag_Key) as? Int{
                return v
            }
            self.hudVersion = 0
            return 0
        }
        set {
            objc_setAssociatedObject(self.base, &XX_Ex_Tag_Key, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
}


public class HUD {
    /// show(withStatus
    @discardableResult
    public static func show(status:String,relate:NSObject? = nil)->Int?{
        
        if #available(iOS 13.0, *) {
            ProgressHUD.show(status)
        } else {
            SVProgressHUD.show(withStatus: status)
        }
        
        relate?.xx.hudVersion += 1
        return relate?.xx.hudVersion
    }
    
    public static func dismiss(relate:NSObject? = nil,version:Int? = nil){
        if let relate = relate ,let version = version{
            // 已经不是一个版本了 所以不需要关闭
            if relate.xx.hudVersion != version {
                return
            }
        }
        
        if #available(iOS 13.0, *) {
            ProgressHUD.dismiss()
        } else {
            SVProgressHUD.dismiss()
        }
        
    }
    
    private static func uploadVersion(relate:NSObject?){
        if let relate = relate{
            // 提升一个版本
            relate.xx.hudVersion += 1
        }
    }
    
    public static func showInfo(status:String,relate:NSObject? = nil){
        uploadVersion(relate: relate)
        if #available(iOS 13.0, *) {
            ProgressHUD.show(status,icon: AlertIcon.message)
        } else {
            SVProgressHUD.showInfo(withStatus: status)
        }
        
    }
    
    public static func showSuccess(status:String,relate:NSObject? = nil){
        uploadVersion(relate: relate)
        if #available(iOS 13.0, *) {
            ProgressHUD.showSucceed(status, interaction: true)
        } else {
            SVProgressHUD.showSuccess(withStatus: status)
        }
        
    }
    
    public static func showError(status:String,relate:NSObject? = nil){
        uploadVersion(relate: relate)
        if #available(iOS 13.0, *) {
            ProgressHUD.showError(status, interaction: true)
        } else {
            SVProgressHUD.showError(withStatus: status)
        }
        
    }
    
    public static func showProgress(status:String?,progress:Float){
        if #available(iOS 13.0, *) {
            ProgressHUD.showProgress(status, CGFloat(progress))
        } else {
            SVProgressHUD.showProgress(progress,status: status)
        }
    }
    
    public static func config(){
        
        if #available(iOS 13.0, *) {
            
        } else {
            //    ProgressHUD.a
            SVProgressHUD.setDefaultStyle(.dark)
            SVProgressHUD.setMinimumSize(.init(width: 100, height: 80))
            SVProgressHUD.setMinimumDismissTimeInterval(1.5)
        }
        
    }
    
}
